package com.shou.domain.park.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.domain.common.AbstractService;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.park.model.ParkingLotEntity;
import com.shou.domain.park.repository.IParkingLotRepository;
import com.shou.domain.park.service.IParkingLotService;
import com.shou.types.common.TableNameEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Nemo
* @description 针对表【parkinglots(停车场表，记录停车场基本信息)】的数据库操作Service实现
* @createDate 2025-02-07 22:10:51
*/
@Service
public class ParkingLotService extends AbstractService implements IParkingLotService {
    @Resource
    private IParkingLotRepository parkingLotRepository;

    @Override
    public void insertParkingLot(ParkingLotEntity parkingLotEntity) {
        validateParkingLotEntity(parkingLotEntity);
        parkingLotRepository.insert(parkingLotEntity);
    }

    @Override
    public ParkingLotEntity getParkingLotById(Integer id) {
        return parkingLotRepository.getParkingLotById(id);
    }

    @Override
    public IPage<ParkingLotEntity> pageQuery(PageQueryEntity pageQueryEntity) {
        pageQueryEntity.setSortName(camelToSnake(pageQueryEntity.getSortName()));
        validateQueryParams(pageQueryEntity, TableNameEnum.PARKING_LOT);
        return parkingLotRepository.getParkingLotsPage(pageQueryEntity);
    }

    @Override
    public void updateParkingLotById(ParkingLotEntity parkingLotEntity) {
        Integer id = parkingLotEntity.getId();
        if(id == null || !checkIfExistById(id, TableNameEnum.PARKING_LOT)){
            throw new RuntimeException("id为" + id + "的记录不存在");
        }
        validateParkingLotEntity(parkingLotEntity, parkingLotEntity.getId());
        parkingLotRepository.updateParkingLotById(parkingLotEntity);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        super.deleteByIds(ids, TableNameEnum.PARKING_LOT);
    }

    @Override
    public void deleteAll() {
        super.deleteAll(TableNameEnum.PARKING_LOT);
    }

    private void validateParkingLotEntity(ParkingLotEntity parkingLot, Integer... excludeIds) {
        validateCode(parkingLot.getCode(), TableNameEnum.PARKING_LOT.getEnTableName(), excludeIds);
    }
}




